package com.lw.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.lw.ssm.dto.R;
import com.lw.ssm.pojo.Course;
import com.lw.ssm.service.CourseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2022/3/10 17:10
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    private CourseService courseService;

    private Logger logger = LoggerFactory.getLogger(CourseController.class);

    @Autowired
    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }

    @GetMapping("/find/{id}")
    public R findById(@PathVariable Long id){
        logger.error("根据id查询方法执行了，当前id为{}",id);
        Course course = courseService.findById(id);
        return R.setOK(course);
    }

    @GetMapping("/find")
    public R findAll(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "2") int pageSize){
        PageInfo<Course> pageInfo = courseService.findAll(pageNum, pageSize);
        logger.error("根据分页查询执行了,");
        return R.setOK(pageInfo);
    }

    @PostMapping("/add")
    public R addCourse(@RequestBody Course course){
        courseService.addCourse(course);
        return R.setOK();
    }

    @PostMapping("/update")
    public R updateCourse(@RequestBody Course course){
        courseService.updateCourse(course);
        return R.setOK();
    }

    @GetMapping("/del/{id}")
    public R deleteCourse(@PathVariable Long id){
        courseService.deleteCourse(id);
        return R.setOK();
    }
}
